/* ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
   Module Name          : com.yaodian100.mall.shopping.web.OrderInfoAction
   Module Description   :

   Date Created      : 2010/1/11
   Original Author   : ellen.li
   Team              : yaodian100
   ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
   MODIFICATION HISTORY
   ------------------------------------------------------------------------------
   Date Modified       Modified by       Comments
   ------------------------------------------------------------------------------
   ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/
package com.yaodian100.mall.shopping.web;

import java.math.BigDecimal;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.xwork.StringUtils;
import org.apache.struts2.ServletActionContext;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.Preparable;
import com.yaodian100.core.common.dao.impl.CommonCriteria;
import com.yaodian100.core.common.service.BaseManager;
import com.yaodian100.core.common.service.DomainService;
import com.yaodian100.core.common.utils.DateUtil;
import com.yaodian100.core.common.utils.ServerValue;
import com.yaodian100.core.menu.entity.Menu;
import com.yaodian100.ec.campaign.entity.Campaign;
import com.yaodian100.ec.campaign.entity.CampaignDiscount;
import com.yaodian100.ec.campaign.entity.CampaignProduct;
import com.yaodian100.ec.campaign.service.CampaignDiscountService;
import com.yaodian100.ec.campaign.service.CampaignProductService;
import com.yaodian100.ec.campaign.service.CampaignService;
import com.yaodian100.ec.category.entity.Category;
import com.yaodian100.ec.category.service.CategoryService;
import com.yaodian100.ec.cobranding.entity.DPNStore;
import com.yaodian100.ec.cobranding.service.DPNService;
import com.yaodian100.ec.coupon.entity.CouponItem;
import com.yaodian100.ec.coupon.service.CouponService;
import com.yaodian100.ec.delivery.service.DeliveryRuleService;
import com.yaodian100.ec.givebonus.entity.BonusSetting;
import com.yaodian100.ec.givebonus.service.BonusSettingService;
import com.yaodian100.ec.member.entity.AddressBook;
import com.yaodian100.ec.member.entity.Balance;
import com.yaodian100.ec.member.entity.Member;
import com.yaodian100.ec.member.service.AddessService;
import com.yaodian100.ec.member.service.MemberService;
import com.yaodian100.ec.payment.service.TransferService;
import com.yaodian100.ec.product.entity.Product;
import com.yaodian100.ec.product.entity.Specification;
import com.yaodian100.ec.product.service.ProductService;
import com.yaodian100.ec.shelf.entity.Shelf;
import com.yaodian100.ec.shelf.service.ShelfService;
import com.yaodian100.ec.shopping.entity.CartOrderProduct;
import com.yaodian100.ec.shopping.entity.CartOrderProductDetail;
import com.yaodian100.ec.shopping.entity.CartOrders;
import com.yaodian100.ec.shopping.service.CartOrdersService;
import com.yaodian100.ec.timelimitsale.service.TimeLimitSaleService;
import com.yaodian100.mall.member.util.UserData;

/**
 * @author ellen.li
 * 
 */
public class CopyOfOrderInfoAction extends ActionSupport implements Preparable {

	/**
	 * 填寫訂購訊息
	 */
	private static final long serialVersionUID = 3715704720458124336L;
	private final Logger logger = LoggerFactory.getLogger(CopyOfOrderInfoAction.class);
	private CampaignProductService campaignProductService;
	private CartOrdersService cartordersService;
	private CategoryService categoryService;
	private MemberService memberService;
	private CouponService couponService;
	private ProductService productService;
	private ShelfService shelfService;
	private TransferService transferService;
	@Autowired
	private CampaignService campaignService;
	@Autowired
	private DeliveryRuleService deliveryRuleService;
	@Autowired
	private CampaignService campaignService;
	private List<AddressBook> abList;
	private Member member;
	private UserData userData;
	private String abid;
	private AddressBook addressBook;
	private String adbk;// 判斷是否由購物車或地址簿來的flag
	private AddressBook addBookDataJson;
	private AddressBook lastAddressBook;
	private CommonCriteria crit = new CommonCriteria();
	private List<CouponItem> couponitems;
	private DomainService<Menu> menuService; // 與 setter
	private Menu productTpMenu; // 與 getter, setter
	private AddessService addressService;
	private String actionResult;// cod json return
	private String codProvince;// cod
	private String codCity;// cod
	private String codDistrict;// cod
	private String id;
	// 頁面參數
	private String contactName;
	private String province;
	private String city;
	private String district;
	private CartOrders cartOrders;
	private List<CartOrderProductDetail> cartOrderProductDetailList;
	private List<CartOrderProduct> cartOrderProductList;
	private Map<String, List> rsmap = new HashMap<String, List>();
	private String addCmpPd;
	private String orderTotalPrice;
	private List<String> timeLimitCartOrderProductList;// 限時搶購
	private Map<String, String> provincesMap;
	// 購物金
	private Campaign bonusCampaign;
	private int canUse;
	private String overOneThousand = "N";
	private String isBuyNow = "";// 直接購買
	private Map<String, String> resultMap;
	private String cartId;
	private String buyNowCartId;
	private BonusSetting bonusSetting;
	@Resource
	private BaseManager baseMgr;
	@Autowired
	private CampaignDiscountService campaignDiscountService;
	@Autowired
	private TimeLimitSaleService timeLimitSaleService;
	@Autowired
	private BonusSettingService bonusSettingService;
	/**
	 * 活动集合物件 Campaign Entity List
	 */
	private List<CampaignDiscount> campaignList;
	/**
	 * 活动集合物件 Campaign Entity
	 */
	private CampaignDiscount campaignDiscount;
	@Autowired
	private DPNService dpnService;
	private String storeArea;
	// COD﹣收取手續費
	private BigDecimal codFee = new BigDecimal(0);
	private boolean needValidateNum;// 是否需要輸入驗證碼
	private boolean canUseCod;// 是否可使用cod付款
	// 惡意訂購不允許使用COD付款
	private String addr;
	private String mobile;
	private String phone;
	// 检核是否做过手机验证
	private boolean ismobileVaild;
	private Map<String, String> prodVolumeMap;// 大材积商品

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.opensymphony.xwork2.Preparable#prepare()
	 */

	/**
	 * 查询收货地址
	 * 
	 * @return
	 * @throws Exception
	 */
	public String findAddBook() throws Exception {
		if (StringUtils.isNotBlank(abid) && adbk.equals("Y")) {
			addBookDataJson = memberService.getAddressBook(Long.parseLong(abid));
			setContactName(addBookDataJson.getContactName());
			setCity(addBookDataJson.getCity());
			setDistrict(addBookDataJson.getDistrict());
			setProvince(addBookDataJson.getProvince());
		}
		return "jsonGetAddBook";
	}

	/**
	 * 查询达芙妮门市
	 * 
	 * @return
	 * @throws Exception
	 */
	public String findDaphnes() throws Exception {
		if (StringUtils.isNotBlank(storeArea)) {
			CommonCriteria crit = new CommonCriteria();
			crit.addEq("storeArea", storeArea);
			List<DPNStore> DPNStores = dpnService.getDPNStoretList(crit);
			actionResult = StringUtils.defaultString(actionResult);
			for (DPNStore dpnstore : DPNStores) {
				actionResult = actionResult + dpnstore.getStoreId() + "," + dpnstore.getStoreAddress() + "("
						+ dpnstore.getStoreName() + "-" + dpnstore.getStoreType() + ")" + "," + dpnstore.getStoreLatitude() + ","
						+ dpnstore.getStoreLongitude() + "$";
			}
			if (actionResult.length() > 0) {
				actionResult = actionResult.substring(0, actionResult.length() - 1);
			}
		}
		return "jsonGetDaphnes";
	}

	public String getAbid() {
		return abid;
	}

	public List<AddressBook> getAbList() {
		return abList;
	}

	public String getActionResult() {
		return actionResult;
	}

	public String getAdbk() {
		return adbk;
	}

	public AddressBook getAddBookDataJson() {
		return addBookDataJson;
	}

	public String getAddCmpPd() {
		return addCmpPd;
	}

	public AddressBook getAddressBook() {
		return addressBook;
	}

	public int getCanUse() {
		return canUse;
	}

	public String getCartId() {
		return cartId;
	}

	public List<CartOrderProductDetail> getCartOrderProductDetailList() {
		return cartOrderProductDetailList;
	}

	public List<CartOrderProduct> getCartOrderProductList() {
		return cartOrderProductList;
	}

	public CartOrders getCartOrders() {
		return cartOrders;
	}

	public CartOrdersService getCartordersService() {
		return cartordersService;
	}

	public String getCity() {
		return city;
	}

	public String getCodCity() {
		return codCity;
	}

	public String getCodDistrict() {
		return codDistrict;
	}

	public String getCodProvince() {
		return codProvince;
	}

	public String getContactName() {
		return contactName;
	}

	public List<CouponItem> getCouponitems() {
		return couponitems;
	}

	public CouponService getCouponService() {
		return couponService;
	}

	public String getDistrict() {
		return district;
	}

	// 判断运费
	public String getFreight() throws Exception {
		// String[] getBuyNowFlag = isBuyNow.split("_");
		HttpServletRequest request = (HttpServletRequest) ActionContext.getContext().get(ServletActionContext.HTTP_REQUEST);
		HttpServletResponse response = (HttpServletResponse) ActionContext.getContext().get(
				ServletActionContext.HTTP_RESPONSE);
		String finalMustPay = StringUtils.defaultString(request.getParameter("finalMustPay"), "0.00");
		// String addCmpPd = StringUtils.defaultString(request.getParameter("addCmpPd"), "");// 滿額購
		// if (getBuyNowFlag[0].equals("Y")) {// 是否是直接購買
		// BigDecimal totoalWeight = new BigDecimal(0);
		// String products = StringUtils.defaultString(request.getParameter("chkInveny"), "");// 滿額購
		// String[] spProducts = products.split(";");
		// for (int iP = 0; iP < spProducts.length; iP++) {
		// Product product = productService.get(spProducts[iP].split(",")[0]);
		// if (spProducts[iP].split(",")[3].equals("0") || spProducts[iP].split(",")[3].equals("1")
		// || spProducts[iP].split(",")[3].equals("6")) {
		// totoalWeight = totoalWeight.add(new BigDecimal(spProducts[iP].split(",")[2]).multiply(product
		// .getProductWeight()));
		// } else {
		// totoalWeight = totoalWeight.add(product.getProductWeight());
		// }
		// }
		// BigDecimal www = addressService.getFreight(codProvince, totoalWeight, new BigDecimal(finalMustPay));
		// actionResult = String.valueOf(www);
		// } else {
		UserData userData = new UserData(request, response);
		String memberId = StringUtils.defaultString(userData.getMemberId());
		cartOrders = cartordersService.getCart(memberId, null);
		Map<String, String> optional = new HashMap<String, String>();
		optional.put("domain", request.getServerName());
		optional.put("cookie", userData.getApplySource());
		cartOrders = cartordersService.calculateCart(cartOrders, optional);
		BigDecimal www = addressService.getFreight(codProvince, cartOrders.getWeightSum().multiply(new BigDecimal("1000")),
				new BigDecimal(finalMustPay));
		actionResult = String.valueOf(www);
		// }
		return "jsonGetFreight";
	}

	public String getIsBuyNow() {
		return isBuyNow;
	}

	public Member getMember() {
		return member;
	}

	public MemberService getMemberService() {
		return memberService;
	}

	public String getOverOneThousand() {
		return overOneThousand;
	}

	public ProductService getProductService() {
		return productService;
	}

	public Menu getProductTpMenu() {
		return productTpMenu;
	}

	public String getProvince() {
		return province;
	}

	public Map<String, String> getResultMap() {
		return resultMap;
	}

	public Map<String, List> getRsmap() {
		return rsmap;
	}

	public ShelfService getShelfService() {
		return shelfService;
	}

	// 判断是否可货到付款
	public String isCod() throws Exception {
		boolean www = addressService.isCod(codCity, codDistrict);
		actionResult = String.valueOf(www);
		return "jsonIsCod";
	}

	// 从收货地址簿选取
	public String loadMyAddBook() throws Exception {
		HttpServletRequest request = (HttpServletRequest) ActionContext.getContext().get(ServletActionContext.HTTP_REQUEST);
		HttpServletResponse response = (HttpServletResponse) ActionContext.getContext().get(
				ServletActionContext.HTTP_RESPONSE);
		userData = new UserData(request, response);
		Map<String, String> drMap = deliveryRuleService.findDeliveryType("cart", id);
		String deliveryType = drMap.get("deliveryType");
		if (StringUtils.equalsIgnoreCase(ServerValue.getScmType(), deliveryType)) {
			abList = memberService.getCanUseAddressBook(userData.getMemberId(), deliveryType, drMap.get("fromProvince"),
					drMap.get("fromCity"), drMap.get("fromDistrict"));
		} else {
			// 大材积
			Map<String, String> prodVolumeMap = cartordersService.chkVolumeOfCartOrderDetail(id);
			if (prodVolumeMap.size() > 0) {
				abList = memberService.getCanUseAddressBook(userData.getMemberId(), deliveryType, "", "", "", true);
			} else {
				abList = memberService.getCanUseAddressBook(userData.getMemberId(), deliveryType, "", "", "");
			}
		}
		return "myAddBook";
	}

	/*
	 * 呼叫可使用的禮卷
	 */
	public String loadMyCoupon() throws Exception {
		String[] getBuyNowFlag = isBuyNow.split("_");
		HttpServletRequest request = (HttpServletRequest) ActionContext.getContext().get(ServletActionContext.HTTP_REQUEST);
		HttpServletResponse response = (HttpServletResponse) ActionContext.getContext().get(
				ServletActionContext.HTTP_RESPONSE);
		userData = new UserData(request, response);
		member = memberService.get(userData.getMemberId());
		CommonCriteria crit = new CommonCriteria();
		crit.addEq("memberId", member.getMemberId());
		couponitems = couponService.getItems(member.getMemberId());
		int couponitemIndex = 0;
		if (getBuyNowFlag[0].equals("Y")) {// 是否是直接購買
			if (couponitems.size() > 0) {
				for (CouponItem couponitem : couponitems) {
					StringBuilder sb = new StringBuilder();
					Product product = productService.get(getBuyNowFlag[1]);
					Category cg = categoryService.get(Long.parseLong(getBuyNowFlag[3]));
					Shelf shelf = shelfService.getActiveShelfByProductId(getBuyNowFlag[1]);
					// 現行禮券規劃加入限額功能: 當商品售價 (unit_price) 超過設定之限額時, 才允許使用該禮券
					BigDecimal couponMinPrice = couponitems.get(couponitemIndex).getCoupon().getMinimumProductPrice();
					List<String> coupList = new ArrayList();
					if (couponService.isValid(couponitem, product.getProductId(), cg.getCategoryPath())) {
						if (null != couponMinPrice) {
							if (couponMinPrice.compareTo(shelf.getShelfPrice()) <= 0) {
								coupList.add(product.getProductId());
								coupList.add(product.getProductName());
								coupList.add(shelf.getShelfPrice().toString());
								coupList.add(couponitem.getUuid());
								coupList.add(couponitem.getCoupon().getUuid());
								rsmap.put(couponitem.getCode(), coupList);
							}
						}
					}
					couponitemIndex++;
				}
			}
		} else {
			// 購物車購買
			if (couponitems.size() > 0 && cartOrderProductList.size() > 0) {
				for (CouponItem couponitem : couponitems) {
					StringBuilder sb = new StringBuilder();
					List<String> coupList = new ArrayList();
					for (CartOrderProduct cartorderProduct : cartOrderProductList) {
						Product product = productService.get(cartorderProduct.getProdId());
						if (!Arrays.asList("5", "2").contains(cartorderProduct.getProdTp())) {// 滿額購,贈品商品不能加進coupon計算
							Shelf shelf = shelfService.getActiveShelfByProductId(cartorderProduct.getProdId());
							// 現行禮券規劃加入限額功能: 當商品售價 (unit_price) 超過設定之限額時, 才允許使用該禮券
							BigDecimal couponMinPrice = couponitems.get(couponitemIndex).getCoupon().getMinimumProductPrice();
							if (couponService.isValid(couponitem, cartorderProduct)) {
								if (null != couponMinPrice) {
									if (couponMinPrice.compareTo(shelf.getShelfPrice()) <= 0) {
										coupList.add(product.getProductId());
										coupList.add(product.getProductName());
										coupList.add(shelf.getShelfPrice().toString());
										coupList.add(couponitem.getUuid());
										coupList.add(couponitem.getCoupon().getUuid());
									}
								}
							}
						}
					}
					rsmap.put(couponitem.getCode(), coupList);
					couponitemIndex++;
				}
			}
		}
		return "myCoupon";
	}

	public String orderInfo() throws Exception {
		HttpServletRequest request = (HttpServletRequest) ActionContext.getContext().get(ServletActionContext.HTTP_REQUEST);
		HttpServletResponse response = (HttpServletResponse) ActionContext.getContext().get(
				ServletActionContext.HTTP_RESPONSE);
		UserData userData = new UserData(request, response);
		logger.info("getAddCmpPd():{}", getAddCmpPd());
		String cmpPordId = "";
		if (StringUtils.isNotBlank(getAddCmpPd())) {
			cmpPordId = getAddCmpPd().split(",")[0];
		}
		cartId = userData.readRawValue("cartid");
		logger.info("xxxxxxxxxxxxxxxbuyNowCartId:{}", buyNowCartId);
		String memberId = userData.getMemberId();
		String currentCartId = cartId;
		if (memberId != null) {
			currentCartId = memberId;
		}
		// 商品重量
		cartOrderProductList.clear();
		//
		String hql = "from CartOrderProduct where cartId=? and qty>=?";
		List paramsList = new ArrayList();
		paramsList.add(currentCartId);
		paramsList.add(new Integer(1));
		Object[] results = baseMgr.queryByHql(hql, paramsList.toArray());
		for (Object obj : results) {
			CartOrderProduct cop = (CartOrderProduct) obj;
			cartOrderProductList.add(cop);
		}

		cartOrderProductDetailList.clear();
		hql = "from CartOrderProductDetail where cartId=? and qty>=? order by cartOrderProdId, cartOrderDetailId asc";
		paramsList = new ArrayList();
		paramsList.add(currentCartId);
		paramsList.add(new Integer(1));
		results = baseMgr.queryByHql(hql, paramsList.toArray());
		for (Object obj : results) {
			CartOrderProductDetail cod = (CartOrderProductDetail) obj;
			cartOrderProductDetailList.add(cod);
		}

		// resultMap = cartordersService.showProductWeight(cartOrderProductDetailList, cmpPordId);
		// cartOrders=(CartOrders)baseMgr.get(CartOrders.class, currentCartId);
		cartOrders = cartordersService.getCart(memberId, currentCartId);
		Map<String, String> optional = new HashMap<String, String>();
		optional.put("domain", request.getServerName());
		optional.put("cookie", userData.getApplySource());
		cartOrders = cartordersService.calculateCart(cartOrders, optional);
		if (StringUtils.isNotBlank(addCmpPd)) {
			// 查询满额购的售价
			crit = new CommonCriteria();
			crit.addEq("productId", addCmpPd.split(",")[0]);
			CampaignProduct campaignProduct = campaignProductService.getList(0, 1, crit, null).get(0);
			// 抓取價格
			Shelf cmpShelf = shelfService.getActiveShelfByProductId(addCmpPd.split(",")[0]);
			addCmpPd = addCmpPd + "," + campaignProduct.getPrice().setScale(2);
			// 查询加购商品的名称
			Product cmpProduct = productService.get(addCmpPd.split(",")[0]);
			addCmpPd = addCmpPd + "," + cmpProduct.getProductName();
			// 查询商品属性
			Specification cmpSpec = productService.getSpecification(Long.parseLong(addCmpPd.split(",")[1]));
			addCmpPd = addCmpPd + "," + cmpSpec.getSpecificationName();
			cartOrders.setProductSum(cartOrders.getProductSum() + 1);

			Product campProduct = (Product) baseMgr.get(Product.class, campaignProduct.getProductId());
			request.setAttribute("campProductWeight", campProduct.getProductWeight());
		}
		cartOrderProductList = cartOrders.getCartOrderProducts();

		// 限時搶購
		if (timeLimitSaleService.findTimeLimitSale(cartOrderProductList) != null) {
			if (timeLimitSaleService.findTimeLimitSale(cartOrderProductList).size() > 0) {
				timeLimitCartOrderProductList = timeLimitSaleService.findTimeLimitSale(cartOrderProductList);
			}
		}
		// 查最新聯絡人
		Map<String, String> drMap = deliveryRuleService.findDeliveryType("cart", cartOrders.getCartId());
		String deliveryType = drMap.get("deliveryType");
		if (StringUtils.equalsIgnoreCase(ServerValue.getScmType(), deliveryType)) {
			abList = memberService.getCanUseAddressBook(memberId, deliveryType, drMap.get("fromProvince"), drMap
					.get("fromCity"), drMap.get("fromDistrict"));
		} else {
			abList = memberService.getCanUseAddressBook(memberId, deliveryType, "", "", "");
		}
		if (abList.size() > 0) {
			lastAddressBook = abList.get(0);
			setCity(lastAddressBook.getCity());
			setDistrict(lastAddressBook.getDistrict());
			setProvince(lastAddressBook.getProvince());
		}
		// 檢查是否订单金额与预期不符
		// orderTotalPrice = orderTotalPrice.substring(0, orderTotalPrice.indexOf(".") + 2);
		// System.out.println(finalTotalAmount);

		BigDecimal amountFromPage = new BigDecimal((orderTotalPrice)).setScale(1);

		BigDecimal cartSumAmount = cartOrders.getAmount().add(cartOrders.getDiscount());

		if (cartOrders.getCartOrderProducts() == null || cartOrders.getCartOrderProducts().size() == 0
				|| Math.abs(cartSumAmount.subtract(amountFromPage).floatValue()) != 0) {
			throw new CartException("订单金额与预期不符，请检查购物车！");
		}

		// 購物金使用限制
		if (null != userData && null != userData.getMemberId()) {
			member = memberService.get(userData.getMemberId());
			if (null != member) {
				bonusCampaign = campaignService.getActiveBonusCampaignByM02(optional);
				if (null != bonusCampaign) {
					bonusCampaign.getJsonDetail();
					// 將積分換算為錢,可使用多少積分
					Balance balance = member.getBalance();
					canUse = balance.getBonusBalance().intValue() / Integer.parseInt(bonusCampaign.getOffsetRate());
				}
				// overOneThousand = transferService.checkOverOneThousandBonus(member.getMemberId());
				overOneThousand = "N";
			} else {
				canUse = 0;
			}

		}

		// COD﹣收取手續費
		codFee = addressService.getCodFee();

		// 是否需要輸入驗證碼
		needValidateNum = cartordersService.needValidateNum(cartOrders.getCartId());

		// 地址簿-省
		provincesMap = deliveryRuleService.getCartProvinces(cartOrders.getCartId());

		// 限制COD付款
		canUseCod = deliveryRuleService.chkProdDetailCanUseCod("cart", cartOrders.getCartId());

		// 检核是否做过手机验证
		ismobileVaild = member.getIsMobileMember();

		// 大材积商品
		prodVolumeMap = cartordersService.chkVolumeOfCartOrderDetail(cartOrders.getCartId());
		return "orderInfo";
	}

	public void prepare() throws Exception {
		cartOrderProductList = new ArrayList<CartOrderProduct>();
		cartOrderProductDetailList = new ArrayList<CartOrderProductDetail>();
		clearErrorsAndMessages();
		if (productTpMenu == null) {
			productTpMenu = menuService.get("Product.Type");
		}
		HttpServletRequest request = (HttpServletRequest) ActionContext.getContext().get(ServletActionContext.HTTP_REQUEST);
		HttpServletResponse response = (HttpServletResponse) ActionContext.getContext().get(
				ServletActionContext.HTTP_RESPONSE);
		UserData userData = new UserData(request, response);
		if (null != userData && null != userData.getMemberId()) {
			member = memberService.get(userData.getMemberId());
		}
		String tmpCartId = userData.readRawValue("cartid");
		String currentCartId = tmpCartId;
		if (member != null) {
			currentCartId = member.getMemberId();
		}

		// String
		// hql="from CartOrderProductDetail where qty>=? and cartId=? order by cartOrderProdId, cartOrderDetailId asc";
		String hql = "from CartOrderProductDetail where qty>=? and cartId=? order by cartOrderProdId, cartOrderDetailId asc";
		List paramsList = new ArrayList();
		paramsList.add(new Integer(1));
		paramsList.add(currentCartId);
		Object[] results = baseMgr.queryByHql(hql, paramsList.toArray());
		for (Object obj : results) {
			CartOrderProductDetail cod = (CartOrderProductDetail) obj;
			cartOrderProductDetailList.add(cod);
		}
		hql = "from CartOrderProduct where qty>=? and cartId=? order by cartOrderProdId desc";
		results = baseMgr.queryByHql(hql, paramsList.toArray());
		for (Object obj : results) {
			CartOrderProduct cop = (CartOrderProduct) obj;
			cartOrderProductList.add(cop);
		}
		if (member != null) {
			// 購物金
			Balance balance = member.getBalance();
			canUse = balance.getBonusBalance().intValue();
			// overOneThousand = transferService.checkOverOneThousandBonus(member.getMemberId());
		}
		overOneThousand = "N";
		// 修改贈品數量
		/*
		 * int mainProductQty = 0; for (CartOrderProduct cop : cartOrderProductList) { for (CartOrderProductDetail copd :
		 * cartOrderProductDetailList) { if (copd.getCartOrderProdId().equals(cop.getCartOrderProdId())) { mainProductQty =
		 * cop.getQty(); if (copd.getDetailTp().equals("2")) {// 2:贈品 SpecificationInventory specificationInventory = new
		 * SpecificationInventory(); CommonCriteria criteria = new CommonCriteria(); criteria.addEq("productId",
		 * copd.getProdId()); criteria.addEq("specId", Long.parseLong(copd.getProdSpecId())); if
		 * (productService.getSpecificationInventory(criteria).size() > 0) { specificationInventory =
		 * productService.getSpecificationInventory(criteria).get(0); int availableInventory =
		 * specificationInventory.getAvailableInventory() - specificationInventory.getSoldInventory() +
		 * specificationInventory.getCancelInventory(); if (availableInventory < mainProductQty) {
		 * copd.setQty(availableInventory); baseMgr.update(copd); } else { copd.setQty(mainProductQty);
		 * baseMgr.update(copd); } } } } } }
		 */
		// 免运费活动
		CommonCriteria cri = new CommonCriteria();
		DateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		cri.addGe("endDateTime", format.parse(DateUtil.convertDateToString("yyyy-MM-dd HH:mm:ss", DateUtil.getToday()
				.getTime())));
		cri.addLe("startDateTime", format.parse(DateUtil.convertDateToString("yyyy-MM-dd HH:mm:ss", DateUtil.getToday()
				.getTime())));
		cri.addEq("enable", true);
		campaignList = campaignDiscountService.getList(0, -1, cri, null);
		campaignDiscount = new CampaignDiscount();
		if (campaignList.size() > 0) {
			campaignDiscount = campaignList.get(0);
		}
	}

	/*
	 * 手机验证
	 */
	public String loadMobilVaild() throws Exception {
		return "mobilVaild";
	}

	/**
	 * 查询最新收货地址
	 * 
	 * @return
	 * @throws Exception
	 */
	public String findLastAddBook() throws Exception {
		Map<String, String> drMap = deliveryRuleService.findDeliveryType("cart", id);
		String deliveryType = drMap.get("deliveryType");
		if (StringUtils.equalsIgnoreCase(ServerValue.getScmType(), deliveryType)) {
			abList = memberService.getCanUseAddressBook(member.getMemberId(), deliveryType, drMap.get("fromProvince"), drMap
					.get("fromCity"), drMap.get("fromDistrict"));
		} else {
			// 大材积
			Map<String, String> prodVolumeMap = cartordersService.chkVolumeOfCartOrderDetail(id);
			if (prodVolumeMap.size() > 0) {
				abList = memberService.getCanUseAddressBook(member.getMemberId(), deliveryType, "", "", "", true);
			} else {
				abList = memberService.getCanUseAddressBook(member.getMemberId(), deliveryType, "", "", "");
			}
		}
		if (abList.size() > 0) {
			addBookDataJson = abList.get(0);
			setContactName(addBookDataJson.getContactName());
			setCity(addBookDataJson.getCity());
			setDistrict(addBookDataJson.getDistrict());
			setProvince(addBookDataJson.getProvince());
		}
		return "jsonGetAddBook";
	}

	// 惡意訂購不允許使用COD付款
	public String isCodForBadOrder() throws Exception {
		actionResult = memberService.badOrderCheck(member.getMemberId(), addr, mobile, phone);
		if (!"true".equals(actionResult)) {
			actionResult = actionResult + ",true";
		} else {
			actionResult = actionResult + ",false";
		}
		return "jsonIsCod";
	}

	public void setAbid(String abid) {
		this.abid = abid;
	}

	public void setAbList(List<AddressBook> abList) {
		this.abList = abList;
	}

	public void setActionResult(String actionResult) {
		this.actionResult = actionResult;
	}

	public void setAdbk(String adbk) {
		this.adbk = adbk;
	}

	public void setAddBookDataJson(AddressBook addBookDataJson) {
		this.addBookDataJson = addBookDataJson;
	}

	public void setAddCmpPd(String addCmpPd) {
		this.addCmpPd = addCmpPd;
	}

	public void setAddressBook(AddressBook addressBook) {
		this.addressBook = addressBook;
	}

	public void setAddressService(AddessService addressService) {
		this.addressService = addressService;
	}

	public void setCampaignProductService(CampaignProductService campaignProductService) {
		this.campaignProductService = campaignProductService;
	}

	public void setCanUse(int canUse) {
		this.canUse = canUse;
	}

	public void setCartId(String cartId) {
		this.cartId = cartId;
	}

	public void setCartOrderProductDetailList(List<CartOrderProductDetail> cartOrderProductDetailList) {
		this.cartOrderProductDetailList = cartOrderProductDetailList;
	}

	public void setCartOrderProductList(List<CartOrderProduct> cartOrderProductList) {
		this.cartOrderProductList = cartOrderProductList;
	}

	public void setCartOrders(CartOrders cartOrders) {
		this.cartOrders = cartOrders;
	}

	public void setCartordersService(CartOrdersService cartordersService) {
		this.cartordersService = cartordersService;
	}

	public void setCity(String city) {
		this.city = city;
	}

	public void setCodCity(String codCity) {
		this.codCity = codCity;
	}

	public void setCodDistrict(String codDistrict) {
		this.codDistrict = codDistrict;
	}

	public void setCodProvince(String codProvince) {
		this.codProvince = codProvince;
	}

	public void setContactName(String contactName) {
		this.contactName = contactName;
	}

	public void setCouponitems(List<CouponItem> couponitems) {
		this.couponitems = couponitems;
	}

	public void setCouponService(CouponService couponService) {
		this.couponService = couponService;
	}

	public void setDistrict(String district) {
		this.district = district;
	}

	public void setIsBuyNow(String isBuyNow) {
		this.isBuyNow = isBuyNow;
	}

	public void setMember(Member member) {
		this.member = member;
	}

	public void setMemberService(MemberService memberService) {
		this.memberService = memberService;
	}

	public void setMenuService(DomainService<Menu> menuService) {
		this.menuService = menuService;
	}

	public void setOverOneThousand(String overOneThousand) {
		this.overOneThousand = overOneThousand;
	}

	public void setProductService(ProductService productService) {
		this.productService = productService;
	}

	public void setProductTpMenu(Menu productTpMenu) {
		this.productTpMenu = productTpMenu;
	}

	public void setProvince(String province) {
		this.province = province;
	}

	public void setResultMap(Map<String, String> resultMap) {
		this.resultMap = resultMap;
	}

	public void setRsmap(Map<String, List> rsmap) {
		this.rsmap = rsmap;
	}

	public void setShelfService(ShelfService shelfService) {
		this.shelfService = shelfService;
	}

	public void setTransferService(TransferService transferService) {
		this.transferService = transferService;
	}

	public CategoryService getCategoryService() {
		return categoryService;
	}

	public void setCategoryService(CategoryService categoryService) {
		this.categoryService = categoryService;
	}

	public AddressBook getLastAddressBook() {
		return lastAddressBook;
	}

	public void setLastAddressBook(AddressBook lastAddressBook) {
		this.lastAddressBook = lastAddressBook;
	}

	public void setCampaignDiscountService(CampaignDiscountService campaignDiscountService) {
		this.campaignDiscountService = campaignDiscountService;
	}

	public void setCampaignList(List<CampaignDiscount> campaignList) {
		this.campaignList = campaignList;
	}

	public void setCampaignDiscount(CampaignDiscount campaignDiscount) {
		this.campaignDiscount = campaignDiscount;
	}

	public CampaignDiscount getCampaignDiscount() {
		return campaignDiscount;
	}

	public String getOrderTotalPrice() {
		return orderTotalPrice;
	}

	public void setOrderTotalPrice(String orderTotalPrice) {
		this.orderTotalPrice = orderTotalPrice;
	}

	public String getBuyNowCartId() {
		return buyNowCartId;
	}

	public void setBuyNowCartId(String buyNowCartId) {
		this.buyNowCartId = buyNowCartId;
	}

	public String getStoreArea() {
		return storeArea;
	}

	public void setStoreArea(String storeArea) {
		this.storeArea = storeArea;
	}

	public void setDpnService(DPNService dpnService) {
		this.dpnService = dpnService;
	}

	public void setTimeLimitSaleService(TimeLimitSaleService timeLimitSaleService) {
		this.timeLimitSaleService = timeLimitSaleService;
	}

	public List<String> getTimeLimitCartOrderProductList() {
		return timeLimitCartOrderProductList;
	}

	public void setTimeLimitCartOrderProductList(List<String> timeLimitCartOrderProductList) {
		this.timeLimitCartOrderProductList = timeLimitCartOrderProductList;
	}

	public void setBonusSettingService(BonusSettingService bonusSettingService) {
		this.bonusSettingService = bonusSettingService;
	}

	public BonusSetting getBonusSetting() {
		return bonusSetting;
	}

	public void setBonusSetting(BonusSetting bonusSetting) {
		this.bonusSetting = bonusSetting;
	}

	public BigDecimal getCodFee() {
		return codFee;
	}

	public void setCodFee(BigDecimal codFee) {
		this.codFee = codFee;
	}

	public boolean isNeedValidateNum() {
		return needValidateNum;
	}

	public void setNeedValidateNum(boolean needValidateNum) {
		this.needValidateNum = needValidateNum;
	}

	public String getId() {
		return id;
	}

	public void setId(String id) {
		this.id = id;
	}

	public Map<String, String> getProvincesMap() {
		return provincesMap;
	}

	public void setProvincesMap(Map<String, String> provincesMap) {
		this.provincesMap = provincesMap;
	}

	public boolean isCanUseCod() {
		return canUseCod;
	}

	public void setCanUseCod(boolean canUseCod) {
		this.canUseCod = canUseCod;
	}

	public String getAddr() {
		return addr;
	}

	public void setAddr(String addr) {
		this.addr = addr;
	}

	public String getMobile() {
		return mobile;
	}

	public void setMobile(String mobile) {
		this.mobile = mobile;
	}

	public String getPhone() {
		return phone;
	}

	public void setPhone(String phone) {
		this.phone = phone;
	}

	public boolean isIsmobileVaild() {
		return ismobileVaild;
	}

	public void setIsmobileVaild(boolean ismobileVaild) {
		this.ismobileVaild = ismobileVaild;
	}

	public Map<String, String> getProdVolumeMap() {
		return prodVolumeMap;
	}

	public void setProdVolumeMap(Map<String, String> prodVolumeMap) {
		this.prodVolumeMap = prodVolumeMap;
	}

	public Campaign getBonusCampaign() {
		return bonusCampaign;
	}

	public void setBonusCampaign(Campaign bonusCampaign) {
		this.bonusCampaign = bonusCampaign;
	}

}
